package com.example.searchcenter.article;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.searchcenter.R;
import com.example.searchcenter.R.id;
import com.example.searchcenter.R.layout;
import com.example.searchcenter.helper.JSONParser;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class DetailArticle extends Activity {
	
	TextView title;
	TextView article_id;
	TextView sumary;
	TextView detail;
	TextView tag;
	String id_article;
	
	// Progress Dialog
    private ProgressDialog pDialog;
 
    // JSON parser class
    JSONParser jsonParser = new JSONParser();
 
    // single product url
    private static final String url_detail_article = "http://tinhnguyen.edu.vn/apis/getArticleDetail";
    
    // JSON Node names
    static final String TAG_SUCCESS = "success";
    static final String TAG_ARTICLE = "articledetail";
  	static final String TAG_ID_ARTICLE = "cid";
	static final String TAG_DATE = "date";
	static final String TAG_TITLE = "title";
	static final String TAG_CATEGORYID = "categoryid";
	static final String TAG_TITLEASCII = "titleASCII";
	static final String TAG_AUTHORID = "authorId";
	static final String TAG_TAG = "tag";
	static final String TAG_SUMARY = "sumary";
	static final String TAG_DETAILS = "details";
	static final String TAG_SOURCE = "source";
	static final String TAG_THUMBNAILIMAGEURL = "thumbnailImageUrl";
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_article);
		
		// getting article details from intent
        Intent i = getIntent();
 
        // getting article id (pid) from intent
        id_article = i.getStringExtra(TAG_ID_ARTICLE);
        
     // Getting complete article details in background thread
        new GetArticleDetails().execute();
	}
        /**
         * Background Async Task to Get complete article details
         * */
        class GetArticleDetails extends AsyncTask<String, String, String>{
        	/**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(DetailArticle.this);
                pDialog.setMessage("Loading article details. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }
            
            /**
             * Getting article details in background thread
             * */
            protected String doInBackground(String... params){
            	
            	// updating UI from Background Thread
                runOnUiThread(new Runnable(){
                	public void run() {
                		
                		// Check for success tag
                        int success;
                        
                        try{
                        	
                        	// Building Parameters
                            List<NameValuePair> params = new ArrayList<NameValuePair>();
                            params.add(new BasicNameValuePair("id", id_article));
                            
                            // getting product details by making HTTP request
                            // Note that product details url will use GET request
                            JSONObject json = jsonParser.makeHttpRequest(
                            		url_detail_article, "GET", params);
                            
                            // check your log for json response
                            Log.d("Article Details", json.toString());
                            
                            // json success tag
                            success = json.getInt(TAG_SUCCESS);
                            
                            //Toast.makeText(getApplicationContext(),params.toString(), Toast.LENGTH_LONG).show();
                            if(success == 1)
                            {
                            	// successfully received article details
                            	JSONArray articleObj = json.getJSONArray(TAG_ARTICLE); // JSON Array
                            	
                            	// get first article object from JSON Array
                            	JSONObject article = articleObj.getJSONObject(0);
                            	//Toast.makeText(getApplicationContext(),product.toString(), Toast.LENGTH_LONG).show();
                                // article with this pid found
                                // Edit Text
                            	title = (TextView) findViewById(R.id.title_article_detail);
                            	article_id = (TextView) findViewById(R.id.id_article);
                            	sumary = (TextView) findViewById(R.id.sumary_article_detail);
                            	detail = (TextView) findViewById(R.id.detail_article_detail);
                            	tag = (TextView) findViewById(R.id.tag_article_detail);
                            	
                            	// display article data in textview
                            	title.setText(article.getString(TAG_TITLE));
                            	article_id.setText(article.getString(TAG_ID_ARTICLE));
                            	sumary.setText(article.getString(TAG_SUMARY));
                            	detail.setText(article.getInt(TAG_DETAILS));
                            	tag.setText(article.getInt(TAG_TAG));
                				
                            }
                            else
                            {
                            	// not found
                            }
                            
                        }
                        catch(JSONException e ){
                        	e.printStackTrace();
                        }
                	}
                	
                });
                return null;
            	
            }
            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog once got all details
                pDialog.dismiss();
            }
    }
}
